fix(repl): prevent multiline prediction hangs#202
Merged
kunchenguid merged 2 commits intomainfrom Apr 1, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This change fixes a REPL prediction hang caused by entering multiline mode on incomplete input. Previously, that transition cancelled the visible prediction by calling
OnInputChanged(""), which also started a hidden empty-input debounced prediction; if that prediction path stalled, it could keep the REPL tied up and interfere with foreground command execution because interpreter context was shared globally.Risk Assessment: 🟡 Medium — Medium risk: tests are strong and the fix is reasonably bounded, but it changes core cancellation/timeout behavior and includes a real timeout-precedence flaw in prediction requests.
Architecture
Key changes made
PredictionState.Cancel()and uses it for multiline input, so newline transitions invalidate in-flight predictions without launching a replacement empty prediction.ClearContext(), so prediction work and foreground command execution no longer overwrite each other's cancellation state.timeoutfield, and the defaultgsh.models.liteOllama model is configured with15000.docs/tutorial/03-command-prediction.mdso the command prediction tutorial documents the new timeout guidance.How was this tested
go test ./internal/script/interpreter ./internal/repl/input ./internal/repl/predictgo test ./internal/repl/...go test ./cmd/gsh/...go test -run 'TestInterpreter_Context_|TestOpenAIProviderChatCompletion_UsesModelTimeout|TestMultiLineInputDoesNotStartHiddenPrediction' ./internal/script/interpreter ./internal/repl/inputgolangci-lint fmtgolangci-lint run --fixgolangci-lint runCritique Comments
internal/repl/predict/event_provider.go:73 - This outerWithTimeout(..., 30s)caps everyrepl.predictrun, so a user-configured modeltimeoutabove 30s can never take effect. Slow local models will still be cancelled early; derive the limit from model config or make this cap configurable.